﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_CartCheck : System.Web.UI.Page
{
    List<Bestlife.ERP.Entities.Cart_Item> shp = new List<Bestlife.ERP.Entities.Cart_Item>();
    Bestlife.ERP.BusinessRules.Shopping_Cart shoppingCheck;
    protected void Page_Load(object sender, EventArgs e)
    {
        reBind();
        ASPxButton1.Visible = false;

    }

    #region Rebind 
    public void reBind()
    {
        if (Session["cart"] != null)
        {
            shoppingCheck = (Bestlife.ERP.BusinessRules.Shopping_Cart)Session["cart"];
            ASPxGridView1.DataSource = shoppingCheck.Items;
            

            ASPxGridView1.DataBind();
            totp.Visible = false;
            totpv.Visible = false;
            totbv.Visible = false;
            if (shoppingCheck.Items.Count > 0)
            {
                totp.Visible = true;
                totpv.Visible = true;
                totbv.Visible = true;
               totp.Text = "" + shoppingCheck.total;
               totpv.Text = "" + shoppingCheck.totalPoint_value;
               totbv.Text = "" + shoppingCheck.totalBusiness_Value;
            }
        }
    }
    #endregion

    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        Session["cartview"] = null;
        Session["cart"] = null;

        if(Session["cartview"] == null && Session["cart"] == null)
        {
            Response.Redirect("Cart.aspx");
        }
      
    }
    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        ASPxButton1.Visible = true;
        if (Session["user"] != null)
        {
            #region payments

            string Total_Val=totp.Text;

            #endregion

            string u_name = Session["user"].ToString();

            #region Entities And DataAccess Class

            Bestlife.ERP.DataAccess.D_bonus bnus = new Bestlife.ERP.DataAccess.D_bonus();
            Bestlife.ERP.Entities.E_Bonus ebns = new Bestlife.ERP.Entities.E_Bonus();

            Bestlife.ERP.DataAccess.users Us = new Bestlife.ERP.DataAccess.users();
            
            Bestlife.ERP.Entities.E_UserPurchs eupchs = new Bestlife.ERP.Entities.E_UserPurchs();
            Bestlife.ERP.DataAccess.D_UserPurchase dupchs = new Bestlife.ERP.DataAccess.D_UserPurchase();
            

            #endregion

            string UId = Us.GetUserId(u_name); // Change by thanuja (14/06/2012)//change by chathuranga get userId Using user Username 
            uid.Text = UId;

            if (Us.GetUserTablest(UId))
            {
                //have to get bonus values and update !!


                Bestlife.ERP.Entities.E_Bonus bnsback=  bnus.Getbonus(UId);

                #region assign values

                double pv = double.Parse(bnsback.Ppv.ToString());
                double bv = double.Parse(bnsback.Bv.ToString());
                double DbTprice = double.Parse(bnsback.T_sales.ToString());
                double DbTpv=double.Parse(bnsback.Tpv.ToString());
                double DbTbv=double.Parse(bnsback.Tbv.ToString());

                #endregion

                double sTp=DbTprice+double.Parse(totp.Text);
                double sTpv=DbTpv+double.Parse(totpv.Text);
                double sTbv=DbTbv+double.Parse(totbv.Text);
                double sbv=bv+double.Parse(totbv.Text);
                double spv=pv+double.Parse(totpv.Text);

                #region Update Bonus table
                Us.UpdateBonus(UId,sTp,sTpv,sTbv,spv,sbv);
                #endregion

                #region Send Values to calculation_Daily methord

                Bestlife.ERP.DataAccess.calculation_Daily cals = new Bestlife.ERP.DataAccess.calculation_Daily();

                // Pass Values to the calculation_Dailys class :
                cals.calculation_Dailys(UId, double.Parse(totbv.Text), double.Parse(totpv.Text), double.Parse(totp.Text));

               
                #endregion

            }
            else
            {
                #region Insert To Bonus

                Bestlife.ERP.BusinessRules.Check_montlyPV checkmp = new Bestlife.ERP.BusinessRules.Check_montlyPV();
                int userst= checkmp.first_status(UId,double.Parse(totpv.Text));

                ebns.User_id = UId;
                ebns.Tpv = decimal.Parse(totpv.Text);
                ebns.Ppv = decimal.Parse(totpv.Text);
                ebns.Cpv = decimal.Parse("0");
                ebns.T_sales = decimal.Parse(totp.Text);
                ebns.T_commision = decimal.Parse("0");
                ebns.Subdealers_Id = "Not In Use";
                ebns.Bv = decimal.Parse(totbv.Text);
                ebns.User_Status = userst.ToString();
                ebns.Tbv = decimal.Parse(totbv.Text);
                ebns.Storebv = 0;
                ebns.Storepv = 0;
                ebns.Usercreate = UId;
                ebns.Date_create = System.DateTime.Now;
                

                bool st = bnus.Bonus_Add(ebns);
               
                if (st)
                {
                    //msg insert ok
                }
                else
                {
                    // msg server or Sql Error 
                }


                #endregion

                #region Send Values to calculation_Daily methord

                Bestlife.ERP.DataAccess.calculation_Daily cals = new Bestlife.ERP.DataAccess.calculation_Daily();

                // Pass Values to the calculation_Dailys class :
                cals.calculation_Dailys(UId, double.Parse(totbv.Text), double.Parse(totpv.Text), double.Parse(totp.Text));


                #endregion
            }

                #region Insert in to Tbl_UserPurchase

            //Response.WriteFile("www.SampathBankIPG.php/payment-gateway.php");
            //payment gateway information mst be there




            foreach (Bestlife.ERP.Entities.Cart_Item ct in shoppingCheck.Items) 
            {
                eupchs.User_ID = UId;
                eupchs.Proname = ct.Product_Name;
                eupchs.ProID = ct.Produt_ID;
                eupchs.Code = ct.Product_Code;
                eupchs.Qty = ct.Quantity;
                eupchs.Price = decimal.Parse(ct.Product_Price.ToString());
                eupchs.PV = decimal.Parse(ct.Point_Value.ToString());
                eupchs.BV = decimal.Parse(ct.Business_value.ToString());
                eupchs.User_Create = u_name;
                eupchs.Date_Create = System.DateTime.UtcNow.Date;
                eupchs.Last_modify = System.DateTime.Now;
                eupchs.Key_Timestamp = 1;
                bool st=dupchs.UserPurchase_Add(eupchs);


                #region to stock table

                Bestlife.ERP.DataAccess.D_Stock stock = new Bestlife.ERP.DataAccess.D_Stock();
                stock.reduceStock(ct.Product_Name, ct.Quantity);

                #endregion



                if (st)
                {
                    ASPxButton2.Visible = false;
                    conf.Text = "Thank you for your purchase. We appreciate your business. Your order will be processed immediatley upon payment approval from Sampath Bank. At that time, an email confirmation will be sent to the email address you provided to registration. If you made an error entering your email address to registration, your purchase confirmation and tracking information may not arrive. Should you have any questions, please contact us at info@bestlife.com or call us at +94 (0)38 492 7322/ 23/ 24 between 9am and 5pm, Monday through Saturday.";
                //Response.Redirect("Home.aspx");

                //conf.Text = "Welcome To BestLife";
               

                }
            }
            //dupchs.UserPurchase_Add();
            #endregion
          
            

        }
        else 
        {
            Response.Redirect("Login.aspx");
        //redirect
        }
    }
}